

import java.telephony.*;
import java.telephony.events.*;
import java.telephony.media.*;
import java.telephony.media.events.*;
import java.net.*;
import java.util.*;

/* 
 * The MyObserver class implements the CallObserver and 
 * receives all Call-related events. 
 */

public class MyObserver implements CallObserver {

	public void callChangedEvent ( CallEv [  ]  evList )    {  
 
 
 
    for  ( int i = 0; i  <  evList.length; i++ )   {  
 
 
        if  (  ( evList [ i ]  instanceof TermConnEv )  &&  
             ( evList [ i ] .getObserved (  )  != null )  )   {  
            TAPIMediaTerminalConnection termconn = null; 
            String name = null; 
            Terminal term = null; 
            Connection conn = null; 
            TermConnEv tcev = null; 
            KapschProvider prov = null; 
 
 
            try  {  
                tcev =  ( TermConnEv ) evList [ i ] ; 
                termconn =  ( TAPIMediaTerminalConnection ) tcev.getTerminalConnection (  ) ; 
                term = termconn.getTerminal (  ) ; 
                name = term.getName (  ) ; 
             }   
            catch  ( Exception e )   {  
                e.printStackTrace (  ) ; 
             }  
 
 
            if  ( evList [ i ] .getID (  )  == MediaTermConnDtmfEv.ID )   {  
                MediaTermConnDtmfEv ev =  ( MediaTermConnDtmfEv ) evList [ i ] ; 
                char c = ev.getDtmfDigit (  ) ; 
                try  {  
                    if  ( c == \'0\' )   {  
                        URL url = new URL ( \"file:///my.wav\" ) ; 
                        termconn.useRecordURL ( url ) ; 
                        termconn.startRecording (  ) ; 
                     }  
 
 
                    if  ( c == \'1\' )   {  
                        URL url = new URL ( \"file:///my.wav\" ) ; 
                        termconn.usePlayURL ( url ) ; 
                        termconn.startPlaying (  ) ; 
                     }  
 
 
                    if  ( c == \'2\' )   {  
                        termconn.stopRecording (  ) ; 
                     }  
 
 
                    if  ( c == \'3\' )   {  
                        termconn.stopPlaying (  ) ; 
                     }  
 
 
                    if  ( c == \'4\' )   {  
                        URL url = new URL ( \"file:///my1.wav\" ) ; 
                        termconn.useRecordURL ( url ) ; 
                        termconn.startRecording (  ) ; 
                     }  
 
 
                    if  ( c == \'5\' )   {  
                        URL url = new URL ( \"file:///my1.wav\" ) ; 
                        termconn.usePlayURL ( url ) ; 
                        termconn.startPlaying (  ) ; 
                     }  
 
 
                    if  ( c == \'6\' )   {  
                        Address [  ]  addresses; 
                        Address address; 
                        Call call; 
                        Connection [  ]  cn; 
                        String num = \"08111182129\"; 
                        prov =  ( KapschProvider ) term.getProvider (  ) ; 
                        Terminal t = prov.getTerminal ( \"T2\" ) ; 
                        addresses = t.getAddresses (  ) ; 
                        address = addresses [ 0 ] ; 
                        call = prov.createCall (  ) ; 
                        call.addObserver ( new MyOutObserver (  )  ) ; 
                        cn = call.connect ( t, address, num ) ; 
                     }  
 
 
                    if  ( c == \'7\' )   {  
                        Address [  ]  addresses; 
                        Address address; 
                        Call call; 
                        Connection [  ]  cn; 
                        String num = \"0811111849\"; 
                        prov =  ( KapschProvider ) term.getProvider (  ) ; 
                        Terminal t = prov.getTerminal ( \"T2\" ) ; 
                        addresses = t.getAddresses (  ) ; 
                        address = addresses [ 0 ] ; 
                        call = prov.createCall (  ) ; 
                        call.addObserver ( new MyOutObserver (  )  ) ; 
                        cn = call.connect ( t, address, num ) ; 
                     }  
 
 
                    if  ( c == \'8\' )   {  
                        termconn.faxMail ( \"812383\", \"testmail\" ) ; 
                     }  
 
 
                    if  ( c == \'9\' )   {  
                        conn = termconn.getConnection (  ) ; 
                        conn.disconnect (  ) ; 
                     }  
                 }  
                catch  ( Exception e )   {   
                  System.out.println ( \"digit \" + e.toString (  )  ) ;  
                 }  
                // System.out.println ( \"from java: a digit: \" + c ) ;
             }  
 
 
            String msg = \"\\nTerminalConnection to Terminal: \" + name + \" is \"; 
 
 
            if  ( evList [ i ] .getID (  )  == TermConnActiveEv.ID )   {  
                System.out.println ( msg + \"ACTIVE\" ) ; 
 
 
                // set DtmfDetection only for the local site:
                try  {  
                    TAPITermConnEv cEv =  ( TAPITermConnEv ) evList [ i ] ; 
                    Connection [  ]  conns = cEv.call.getConnections (  ) ; 
                    TerminalConnection [  ]  termConns = conns [ 0 ] .getTerminalConnections (  ) ; 
                    TerminalConnection tc = termConns [ 0 ] ; 
                    if  ( tc.equals ( termconn )  )   {  
                        prov =  ( KapschProvider ) term.getProvider (  ) ; 
                        Address addr = termconn.getConnection (  ) .getAddress (  ) ; 
                        TAPIMediaTerminalConnectionCapabilities cap =  ( TAPIMediaTerminalConnectionCapabilities ) prov.getConnectionCapabilities ( term, addr ) ; 
                        if  ( cap.canDetectDtmf (  )  )   {  
                            termconn.setDtmfDetection ( true ) ; 
                         }  
                     }  
                  }  
                catch  ( Exception e )   {   
                    e.printStackTrace (  ) ;  
                 }  
             }  
            else if  ( evList [ i ] .getID (  )  == TermConnRingingEv.ID )   {  
                System.out.println ( msg + \"RINGING\" ) ; 
 
 
                /* Answer the telephone Call */ 
                try  {  
                    termconn.answer (  ) ; 
 
 
                 }   
                catch  ( Exception e )   {  
                    e.printStackTrace (  ) ; 
                 }  
             }  
            else if  ( evList [ i ] .getID (  )  == TermConnDroppedEv.ID )   {  
                System.out.println ( msg + \"DROPPED\" ) ; 
             }  
 
 
 
         }  else if  ( evList [ i ]  instanceof ConnEv )   {  
 
 
            String name = null; 
 
 
            try  {  
                ConnEv cEv =  ( ConnEv ) evList [ i ] ; 
                Connection connection = cEv.getConnection (  ) ; 
                Address addr = connection.getAddress (  ) ; 
                name = addr.getName (  ) ; 
             }   
            catch  ( Exception e )   {  
                e.printStackTrace (  ) ; 
             }  
            String msg = \"\\nConnection to Address: \" + name + \" is \"; 
 
 
            if  ( evList [ i ] .getID (  )  == ConnAlertingEv.ID )   {  
                System.out.println ( msg + \"ALERTING\" ) ; 
             }  
            else if  ( evList [ i ] .getID (  )  == ConnInProgressEv.ID )   {  
                System.out.println ( msg + \"INPROGRESS\" ) ; 
             }  
            else if  ( evList [ i ] .getID (  )  == ConnConnectedEv.ID )   {  
                System.out.println ( msg + \"CONNECTED\" ) ; 
             }  
            else if  ( evList [ i ] .getID (  )  == ConnDisconnectedEv.ID )   {  
                System.out.println ( msg + \"DISCONNECTED\" ) ;  
             }  
         }  
 
 
     }  
 }}